#include "capability.h"
#include "basics/error_handle.h"
#include "basics/todo.h"
#include <sys/capability.h>
#include <sys/prctl.h>
#include <unistd.h>

int set_full_cap() {
    cap_t caps = cap_init();
    check(caps != NULL);
    int num_caps = sizeof(cap_list) / sizeof(cap_value_t);
    int num_drop_caps = sizeof(drop_cap_list) / sizeof(cap_value_t);
    if_fail_return(
        cap_set_flag(caps, CAP_EFFECTIVE, num_caps, cap_list, CAP_SET));
    if_fail_return(cap_set_flag(caps, CAP_EFFECTIVE, num_drop_caps,
                                drop_cap_list, CAP_SET));
    if_fail_return(
        cap_set_flag(caps, CAP_PERMITTED, num_caps, cap_list, CAP_SET));
    if_fail_return(cap_set_flag(caps, CAP_PERMITTED, num_drop_caps,
                                drop_cap_list, CAP_SET));
    if_fail_return(
        cap_set_flag(caps, CAP_INHERITABLE, num_caps, cap_list, CAP_SET));
    if_fail_return(cap_set_flag(caps, CAP_INHERITABLE, num_drop_caps,
                                drop_cap_list, CAP_SET));
    if_fail_return(cap_set_proc(caps));
    if_fail_return(cap_free(caps));
    // caps = cap_get_proc();
    // printf("capability: %s\n", cap_to_text(caps, NULL));
    // cap_free(caps);
    return 0;
}

int default_cap() {
    cap_t caps = cap_init();
    check(caps != NULL);
    // if_fail_return(cap_clear(caps));
    // if_fail_return(cap_set_proc(caps));
    int num_caps = sizeof(cap_list) / sizeof(cap_value_t);
    int num_drop_caps = sizeof(drop_cap_list) / sizeof(cap_value_t);
    if_fail_return(
        cap_set_flag(caps, CAP_EFFECTIVE, num_caps, cap_list, CAP_SET));
    if_fail_return(
        cap_set_flag(caps, CAP_PERMITTED, num_caps, cap_list, CAP_SET));
    if_fail_return(
        cap_set_flag(caps, CAP_INHERITABLE, num_caps, cap_list, CAP_SET));
    if_fail_return(cap_set_proc(caps));
    // for (size_t i = 0; i < num_caps; i++) {
    //   if_fail_return(cap_set_ambient(cap_list[i], CAP_SET));
    // }
    for (size_t i = 0; i < num_drop_caps; i++) {
        if_fail_return(cap_drop_bound(drop_cap_list[i]));
    }
    if_fail_return(cap_free(caps));
    // caps = cap_get_proc();
    // printf("capability: %s\n", cap_to_text(caps, NULL));
    // fflush(0);
    // cap_free(caps);
    return 0;
}

int add_cap(cap_t cap) { todo(); }

int del_cap(cap_t cap) { todo(); }
